import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: "list demo",
      home: MyHomePage(
        title: "首页",
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    // print('_MyHomePageState build');
    return Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: CustomMultiChildLayout(
          delegate: MyLayout(9),
          children: [
            LayoutId(
                id: '1',
                child: const Text(
                  "Wdsf",
                  style: TextStyle(
                      fontSize: 36, textBaseline: TextBaseline.ideographic),
                )),
            LayoutId(
                id: '2',
                child: Container(
                  color: Colors.red,
                )),
          ],
        ));
  }
}

class MyLayout extends MultiChildLayoutDelegate {
  MyLayout(this.thickness);
  double thickness = 8;
  @override
  void performLayout(Size size) {
    Size textSize = layoutChild('1', BoxConstraints.loose(size));
    layoutChild('2', BoxConstraints.tight(Size(textSize.width, thickness)));
    double left = (size.width - textSize.width) / 2;
    double top = (size.height - textSize.height) / 2;
    positionChild('1', Offset(left, top));
    positionChild('2', Offset(left, textSize.height + top));
  }

  @override
  bool shouldRelayout(covariant MultiChildLayoutDelegate oldDelegate) {
    return true;
  }
}
